package com.alpaak.hellospringboot.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Parameter;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;
import java.util.List;

/**
 * @author by ce
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Value("${swagger.title}")
    private String title = "API";

    @Value("${swagger.package}")
    private String basePackage;

    @Value("${swagger.version}")
    private String version = "1.0.0";

    @Value("${swagger.enable}")
    private boolean enableSwagger = false;


    @Bean
    public Docket createRestApi() {
        Docket docket = new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .enable(enableSwagger)
                .select()
                .apis(RequestHandlerSelectors.basePackage(basePackage))
                .paths(PathSelectors.any())
                .build();
        docket.globalOperationParameters(setHeaders());
        return docket;
    }

    private List<Parameter> setHeaders() {
        List<Parameter> params = new ArrayList<>();

        ModelRef strRef = new ModelRef("string");
        params.add(new ParameterBuilder()
                .name("token")
                .description("token")
                .modelRef(strRef)
                .parameterType("header")
                .required(false)
                .defaultValue("3cf47d955362461e95c787bcf81d39f9")
                .build());

        return params;
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title(title)
                //.description("更多Spring Boot相关文章请关注：http://blog.didispace.com/")
                //.termsOfServiceUrl("http://blog.didispace.com/")
                .version(version)
                .build();
    }

}
